#!/bin/bash

# ----------------------------------------------------------------------------
# Copyright 2013 Bloomberg Finance L.P.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ----------------------------- END-OF-FILE ----------------------------------


if [ "$1" = "init" ]
then
    rm -rf coverage
    mkdir -p coverage
    echo > coverage.mk
    echo "init-info =" >> coverage.mk
    echo "gather-info =" >> coverage.mk
    echo "aggregate-info =" >> coverage.mk
    echo "coverage-info = " >> coverage.mk
    for compdir in `find -name \*.gcno -printf "%h\n" | sort -u`
    do
	comp=${compdir##*obj.target/}
	comp=${comp%%/*}
	# skip some targets that do not generate coverage data
	if [ $comp = bsldoc ]; then continue; fi
	if [ $comp = bslmf ]; then continue; fi
	if [ $comp = bslscm ]; then continue; fi
	echo "init-info += coverage/${comp}-initial.info" >> coverage.mk
	echo "coverage/${comp}-initial.info: ${compdir}/*.gcno" >> coverage.mk
	echo -e "\ttools/lcov/bin/lcov --base-directory build --output-file coverage/${comp}-initial.info --capture --initial --directory ${compdir}" >> coverage.mk
	echo >> coverage.mk

	echo "gather-info += coverage/${comp}-test.info" >> coverage.mk
	echo "coverage/${comp}-test.info: ${compdir}/*.gcda" >> coverage.mk
	echo -e "\ttools/lcov/bin/lcov --base-directory build --output-file coverage/${comp}-test.info --capture --directory ${compdir}" >> coverage.mk
	echo >> coverage.mk

	echo "aggregate-info += coverage/${comp}-final.info" >> coverage.mk
	echo "coverage/${comp}-final.info: coverage/${comp}-initial.info coverage/${comp}-test.info" >> coverage.mk
	echo -e "\ttools/lcov/bin/lcov --add-tracefile coverage/${comp}-initial.info --add-tracefile coverage/${comp}-test.info --output-file coverage/${comp}-final.info" >> coverage.mk
	echo >> coverage.mk

	echo "coverage-info += coverage/${comp}-coverage.xml" >> coverage.mk
	echo "coverage/${comp}-coverage.xml: coverage/${comp}-final.info" >> coverage.mk
	echo -e "\tpython tools/lcov_cobertura/lcov_cobertura.py -o coverage/${comp}-coverage.xml --excludes \".*.usr.include.*\" --excludes \"build\" --demangle-cpp coverage/${comp}-final.info" >> coverage.mk
	echo -e "\tsed -i -e \"s/groups\\.bsl\\.//\" coverage/${comp}-coverage.xml" >> coverage.mk
	echo >> coverage.mk
    done
    echo ".PHONY: init gather aggregate coverage" >> coverage.mk
    echo "init: \$(init-info)" >> coverage.mk
    echo "gather: \$(gather-info)" >> coverage.mk
    echo "aggregate: \$(aggregate-info)" >> coverage.mk
    echo "coverage: \$(coverage-info)" >> coverage.mk
    make -f coverage.mk $@
fi

if [ "$1" = "gather" ]
then
    make -f coverage.mk $@
fi

if [ "$1" = "aggregate" ]
then
    make -f coverage.mk $@
fi

if [ "$1" = "coverage" ]
then
    make -f coverage.mk $@
fi
